﻿using B.S.PrimaryProject.Domain;
using B.S.PrimaryProject.ErrorCode;
using B.S.PrimaryProject.API.Applications.Commands.Users;
using B.S.PrimaryProject.API.Dtos;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.PrimaryProject.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        IMediator mediator;
        ILogger<UserController> logger;

        public UserController(IMediator mediator, ILogger<UserController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="userPwd">密码</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<APIResult<UserDto>> Login([FromQuery]string userName,string userPwd)
        {
            try
            {
                LoginCommand cmd=new LoginCommand
                {
                    UserName= userName,
                    UserPwd=userPwd
                };
                logger.LogInformation($"用户名为：{userName}的用户，在{DateTime.Now}登录成功!");
                var result = await mediator.Send(cmd);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("登录出错", ex.Message);
                throw;
            }
        }
    }
}
